package com.onlyphoto.onlymall.widget;


import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onlyphoto.onlymall.R;
import com.onlyphoto.onlymall.base.BaseDialogFragment;
import com.onlyphoto.onlymall.utils.ConvertUtil;
import com.onlyphoto.onlymall.utils.StringUtil;

import java.util.Objects;

/**
 * Created by gaochujia on 2019-09-18.
 */

public class AppUpdateProgressDialog extends BaseDialogFragment {
    private AnimProgressBar progressView;
    private TextView update;
    private LinearLayout llContentContainer;
    private LinearLayout rlFirst, rlSecond;
    private String contents;
    private ImageView close;
    private boolean canClose = true;

    public void setCanClose(boolean canClose) {
        this.canClose = canClose;
    }

    @Override
    protected int getLayout() {
        return R.layout.dialog_update;
    }

    @Override
    protected void initView(View contentView) {
        super.initView(contentView);
        progressView = contentView.findViewById(R.id.download_progress);
        llContentContainer = contentView.findViewById(R.id.ll_content_container);
        close = contentView.findViewById(R.id.iv_close);
        update = contentView.findViewById(R.id.tv_update);
        rlFirst = contentView.findViewById(R.id.rl_first);
        rlSecond = contentView.findViewById(R.id.rl_second);

        getDialog().setCanceledOnTouchOutside(false);

        if (!StringUtil.isBlank(contents)) {
            llContentContainer.removeAllViews();
            TextView temp = new TextView(getActivity());
            // temp.setLineSpacing(0, 1.05f);
            temp.setText(contents);
            temp.setTextSize(12);
            llContentContainer.addView(temp);
        }
        if (null != close && !canClose) {
            close.setVisibility(View.GONE);
        }
        initListener();
    }
    
    private void initListener(){
        update.setOnClickListener(v -> {
            if (null != updateStartCallBack) {
                updateStartCallBack.start();
            }
        });
        close.setOnClickListener(v -> {
            if (canClose) {
                AppUpdateProgressDialog.this.dismiss();
            }
        });
    }

    public void updateProgress(int progress) {
        if (progressView != null) {
            progressView.setProgress(progress);
        }
    }

    public void setTotal(int total) {
        if (progressView != null) {
            progressView.setMax(total);
        }
    }

    public void beginDownload(){
        setCanClose(false);
        rlSecond.setVisibility(View.VISIBLE);
        rlFirst.setVisibility(View.GONE);
    }

    public void setContent(String contents) {
        this.contents = contents;
    }

    private UpdateStartCallBack updateStartCallBack;

    public void setUpdateStartCallBack(UpdateStartCallBack updateStartCallBack) {
        this.updateStartCallBack = updateStartCallBack;
    }

    public interface UpdateStartCallBack {
        void start();
    }

}
